package com.gbcom.autocompile.mail;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import com.gbcom.op.util.RunTimeUtil;

/**
 * 进行email的发送
 * 
 * @author duanxiongwen
 * 
 */
public class SendMails {
	/**
	 * 得到今天的日志文件
	 * 
	 * @return
	 */
	private static String getTodayFileName() {
		// build-2011-07-08.txt
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String destFile = "build-" + dateFormat.format(new Date()) + ".txt";
		return destFile;
	}

	/**
	 * 得到类的路径
	 * 
	 * @return
	 * @throws java.lang.Exception
	 */
	public String getClassPath() throws Exception {
		try {
			String strClassName = getClass().getName();
			String strPackageName = "";
			if (getClass().getPackage() != null) {
				strPackageName = getClass().getPackage().getName();
			}
			String strClassFileName = "";
			if (!"".equals(strPackageName)) {
				strClassFileName = strClassName.substring(
						strPackageName.length() + 1, strClassName.length());
			} else {
				strClassFileName = strClassName;
			}
			URL url = null;
			url = getClass().getResource(strClassFileName + ".class");
			String strURL = url.toString();
			strURL = strURL.substring(strURL.indexOf('/') + 1,
					strURL.lastIndexOf('/'));
			// 返回当前类的路径，并且处理路径中的空格，因为在路径中出现的空格如果不处理的话，
			// 在访问时就会从空格处断开，那么也就取不到完整的信息了，这个问题在web开发中尤其要注意
			return strURL.replaceAll("%20", " ");
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}

	/**
	 * 将程序jar放在哪，就会将vss 放置到哪个盘，默认为E：盘 放送的邮件可以通过main的命令行进行设置
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		// 取得盘符
		SendMails instance = new SendMails();
		String directory = "E:";
		try {
			directory = instance.getClassPath().substring(0, 2);
		} catch (Exception e) {
			e.printStackTrace();

		}
		// 取得tomails
		String[] toMails = args;
		if (args == null || args.length == 1) {
			toMails = new String[] { "dualven@163.com" };
		}
		new Timer().schedule(new compileTask(directory, toMails), new Date(
				2011, 6, 15, 5, 0, 0), 24 * 60 * 60 * 1000);

	}

	private static class compileTask extends TimerTask {
		private String directory = "E:";
		private String[] toMails = null;

		/**
		 * 构造方法
		 */
		public compileTask(String dir, String[] tomail) {
			directory = dir;
			toMails = tomail;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.TimerTask#run()
		 */
		@Override
		public void run() {
			// TODO Auto-generated method stub
			RunTimeUtil.exec("e:\\autoCompile.bat");
			// 取得filelist
			Vector<String> fileList = new Vector<String>();
			fileList.add(directory + "\\vss\\" + getTodayFileName());

			sendMail("duanxiongwen@gbcom.com.cn", "duanxiongwen", "****",
					"10.1.1.248", toMails, " 每日构建的结果 " + getTodayFileName(),
					" 详细结果见附件 " + getTodayFileName(), fileList);
		}

	}

	/**
	 * 发送邮件方法
	 * 
	 * @param sendAddress
	 *            : 发件人地址
	 * @param name
	 *            : 发件人姓名
	 * 
	 * @param password
	 *            : 发件人密码
	 * 
	 * @param emailServer
	 *            : 发送的邮箱服务器地址 例smtp.163.com
	 * @param revAddresses
	 *            : 收件箱地址
	 * @param title
	 *            : 发送邮件的主题
	 * @param content
	 *            ： 发送邮件的内容 可以是超文本标记语言
	 * @param file
	 *            发送的文件列表
	 * @throws MessagingException
	 * @throws AddressException
	 * 
	 */
	private static void sendMail(String sendAddress, String name,
			String password, String emailServer, String[] revAddresses,
			String title, String content, Vector<String> file) {
		// TODO Auto-generated method stub
		try {
			MailNotify.sendSmtpEmail("duanxiongwen@gbcom.com.cn",
					"duanxiongwen", "12311231", "10.1.1.248", revAddresses,
					" 每日构建的结果 " + getTodayFileName(), " 详细结果见附件 "
							+ getTodayFileName(), file);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}
